package com.mud.dnd.ejb;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.common.QueryParameter;
import com.mud.dnd.domain.dao.DnDDAO;
import com.mud.dnd.domain.model.Action;
import com.mud.dnd.domain.model.CardAction;
import com.mud.dnd.domain.model.User;
import com.mud.dnd.ejb.common.AuthenticationException;
import com.mud.dnd.ejb.services.AuthenticationService;


@Stateless
@TransactionManagement
public class ActionManager {
	
	@EJB
	private DnDDAO dao;
	
	@EJB
	private AuthenticationService userService;
	
	@TransactionAttribute
	public List<Action> getActions(List<QueryParameter> params) {
		
		List<Action> actions= dao.getActions(params);
		

		return actions;
	}
	
	
	@TransactionAttribute
	public List<CardAction> getCardActions(List<QueryParameter> params) {
		
		List<CardAction> cardActions= dao.getCardActions(params);
		

		return cardActions;
	}

	@TransactionAttribute
	public int createAction(String name, String description, String pictureURL,
			 String userName, String sessionID) throws DAOException, AuthenticationException {
		
		
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		
		User author = userService.getUserData(userName);
		
		return dao.createAction(name, description, pictureURL,author);

	}
	
	@TransactionAttribute
	public void addActiontoCard(int actionID, int cardID,int amount, String userName, String sessionID) throws DAOException, AuthenticationException
	{
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, cardID))
		{
			throw new AuthenticationException("You can edit only your card.");
			
		}
		
		
		dao.addActiontoCard(actionID, cardID, amount);
		
	}
	
	@TransactionAttribute
	public void updateAction(int id, String name, String description ,
			String pictureURL, int rate, String userName, String sessionID) throws DAOException, AuthenticationException
	{
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, id) && !( name==null && description==null && pictureURL==null && rate!=0))
		{
			throw new AuthenticationException("You can edit only your action.");
			
		}
		
		
		dao.updateAction(id, name, description, pictureURL, null, rate);
	}
	
	@TransactionAttribute
	public void removeActionfromCard(int actionID, int cardID , String userName, String sessionID) throws DAOException, AuthenticationException
	{
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, cardID))
		{
			throw new AuthenticationException("You can edit only your card.");
			
		}
		
		
		dao.removeActionfromCard(actionID, cardID);
	}
	
	
	public void updateActionInCard(int actionID, int cardID ,int amount,int order, String userName, String sessionID) throws DAOException, AuthenticationException
	{
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, cardID))
		{
			throw new AuthenticationException("You can edit only your card.");
			
		}
		
		
		dao.updateActiononCard(actionID, cardID, amount, order);
		
	}
	
	@TransactionAttribute
	public void deleteAction(int actionID , String userName, String sessionID) throws DAOException, AuthenticationException
	{
		
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, actionID))
		{
			throw new AuthenticationException("You can edit only your action.");
			
		}
		
		
		dao.deleteAction(actionID);
		
	}

}
